module MuIDE
class ExtensionList
  attr_accessor :extList,:panel
def initialize(p)
     p1 = Wx::Panel::new p,-1
     @panel = p1
     @extList = ListCtrl::new(p1,Wx::LC_REPORT|Wx::LC_VIRTUAL|Wx::LC_VRULES|Wx::LC_HRULES,p1)
     @extList.insert_column(0,"Name")
     @extList.insert_column(1,"In use?")
     @extList.insert_column(2,"Requires")
     @extList.insert_column(3,"Short Desc.")
     @extList.set_item_count(200)
#      @m_notebook1.set_selection(3) 
#      siz = Wx::BoxSizer::new Wx::VERTICAL
     siz = Wx::StaticBoxSizer::new Wx::VERTICAL,p1,"Single click a item to view its long description\nDouble Click an item to load it.\n"
     siz.add @extList,1,Wx::GROW,Wx::EXPAND
     p1.sizer = siz
     @extList.set_column_width(0,100)
     @extList.set_column_width(1,50)
     @extList.set_column_width(2,220)
     @extList.set_column_width(3,500)
     siz1 = Wx::StaticBoxSizer::new Wx::VERTICAL,p1,"Long descrtion"
     siz.add(siz1,1,Wx::GROW,Wx::EXPAND)
     @extLongDesc = Wx::TextCtrl::new(p1,-1,"",:style=>Wx::TE_MULTILINE)
     siz1.add @extLongDesc,1,Wx::GROW,Wx::EXPAND
       @extList.evt_list_item_selected(@extList.get_id) {|event| on_item_selected(event)}
       @extList.evt_list_item_activated(@extList.get_id) {|event| on_item_activated(event)}
end
  def extend()
     @extentions = []
     a = []
     Dir::open("libs/extentions").each {|f|
       if f =~ /(.*)\.rb/
         @ext = $1
         @long = ""
         @short = ""
         @dlong = false
         @require = ""
         used = "No"
         if @extentions.index($1)
           used = "Yes"
         elsif DEF_EXT.index($1)
             used = "Yes"
             run_ext($1)
         end

         File.open("libs/extentions/#{f}","r") {|file|
           file.each_line("\n") {|line|
             if line =~ /^\# .*ort .*ption\: (.*)/
               @short = $1
# p short;exit
             elsif line =~ /^\# Requires: (.*)/
               @require = $1 
             elsif line =~ /^\# .*ong .*ption:/
               @dlong = true
             elsif line =~ /^\# EOD/
               @dlong = false
             elsif @dlong == true
               @long << line
             end
           }
         }
         a << [@ext,used,@require,@short,@long]
    p @short
p @long
# exit
       end
     }
    
     a.each {|iter|
       node = Node::new(5,NODE_TYPE_EXPANDITEM,@extList)    
       node.iter = iter
#        p iter;exit
       @extList.add_node(node)
       node.unexpand node
     }
      updateList @extList

# p @extList.array
# exit
  end
  def on_item_selected(event)
        @extList.selItem = event.get_index()
        @extList.item = event.get_item()
        @extList.get_column(1,@extList.item)
        index = @extList.indices[@extList.selItem]
#         p "selected index #{index.join(":")}"
        begin
          path = @extList.get_node_path(@extList.selItem)
          node = @extList.get_node(path)
          @extLongDesc.value = node.iter.last
        rescue Exception => e
          p e
          p "believed to be ok and neccessary."
        end
  end
  def run_ext(ext)
         @estrs = ""
         File::open("libs/extentions/#{ext}.rb","r") {|f|
              @estrs = f.readlines.join
#             exit
            }
        if @estrs == ""
        else
          begin
            @extentions << ext
            eval @estrs
          rescue Exception => e
	   err = "Extension Error: #{ext}.rb; #{e.to_s}\n"
	   err += "Platform: #{Wx::PLATFORM}\n"
	   err += "MuIDE Version: #{VERSION}\n"
	   err += "Ruby Version: #{RUBY_VERSION}\n"
	   err += "Backtrace:\n\t"
	   err += e.backtrace.join("\n\t")
	   File.open("extensionMuIDE.err","w") do |fh|
		fh.print(err)
	   end
	  #dir = File.dirname(File.expand_path(__FILE__))
	    if Wx::PLATFORM == "WXMSW"
		Kernel.exec("rubyw #{Dir.getwd}\\libs\\classes\\gui\\wxExtensionError.rb")
	    else
		system("ruby #{Dir.getwd}/libs/classes/gui/wxExtensionError.rb")
	    end
          end
        end

  end
  def on_item_activated(event)
        @extList.selItem = event.get_index()
        @extList.item = event.get_item()
        @extList.get_column(1,@extList.item)
        index = @extList.indices[@extList.selItem]
        
        begin
          path = @extList.get_node_path(@extList.selItem)
          @node = @extList.get_node(path)
          
          if @extentions.index("#{@node.iter[0]}")
          else
            run_ext(@node.iter[0])
            @node.iter[1] = "Yes"
            updateList @extList
           
          end
        rescue
          p "EEERRORRR!"
        end
  end
end
end